<?php

class TagController extends Controller{

    private $M_tags;
    private $M_taghasnews;
    private $M_categoryhastags;

    /**
     * 构造方法
     * 初始化
     */
    public function __construct(){
        $this->M_tags = Model::getInstance('tags');
        $this->M_taghasnews = Model::getInstance('tag has news');
        $this->M_categoryhastags = Model::getInstance('category has tags');
    }

    //模块首页
    public function index(){
        global $controller;

        $curr_page = (int)$this->input()->post('pageNum',1);
        $per_page  = (int)$this->input()->post('numPerPage',30);
        $tag_name = $this->input()->post('tag_name');
        $filter = array();

        if($tag_name){
            $filter['tag_name'] = $tag_name;
        }

        $list = $this->M_tags->getTagList($curr_page,$per_page,$filter);

        $data = array(
            'controller'=>$controller,
            'list'=>$list,
        );

        Template::show('tag/index.php',$data);
    }

    //添加
    public function add(){
        global $controller;

        $data = array(
            'controller'=>$controller
        );

        Template::show('tag/add.php',$data);
    }

    //删除
    public function del(){
        $tag_id = $this->input()->get('tag_id');

        $ret = $this->M_tags->delTag($tag_id);

        if($ret){
            $data = array(
                "statusCode"=>"200",
                "message"=>"删除成功",
                "navTabId"=>"tag"
            );
            //删除标签对应关系
            $this->M_taghasnews->delByTagId($tag_id);
            $this->M_categoryhastags->delByTagId($tag_id);

            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"删除失败",
            );
            echo Response::JSON(300,$data);
        }
    }

    //修改
    public function edit(){
        global $controller;
        $tag_id = $this->input()->get('tag_id');

        $info = $this->M_tags->get($tag_id);

        $data = array(
            'controller'=>$controller,
            'info'=>$info
        );

        Template::show('tag/edit.php',$data);
    }

    //异步添加
    public function ajaxAdd(){

        $tag_name = $this->input()->post('tag_name');

        if(!$tag_name ){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_tags->addTag($tag_name);
        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"添加成功",
                "navTabId"=>"tag",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"添加失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }

    //异步添加
    public function ajaxEdit(){

        $tag_id = $this->input()->post('tag_id');
        $tag_name = $this->input()->post('tag_name');

        if(!$tag_id || !$tag_name){
            $data = array(
                "statusCode"=>"300",
                "message"=>"参数错误，请填写完入库表单！",
            );
            echo Response::JSON(300,$data);
            exit;
        }

        $ret = $this->M_tags->updateTag($tag_id, $tag_name);

        if($ret){

            $data = array(
                "statusCode"=>"200",
                "message"=>"编辑成功",
                "navTabId"=>"tag",
                "callbackType"=>"closeCurrent"
            );
            echo Response::JSON(200,$data);
        }else{

            $data = array(
                "statusCode"=>"300",
                "message"=>"编辑失败",
            );
            echo Response::JSON(300,$data);
        }
        exit;
    }
}